<?php

namespace app\controllers;

use app\models\User;
use app\models\UserMongo;
use Phalcon\Mvc\Controller;

class SiteController extends Controller
{
    public function registerAction()
    {
        if ($this->session->get('user')) {
            throw new \Exception('Your Not Allowed Access This Page!');
        }
        $error = '';
        $user = new User();
        if ($this->request->isPost()) {
            //验证csrf
            if ($this->security->checkToken()) {
                $user->name = $user->username = $this->request->getPost('username');
                $user->password = $this->security->hash($this->request->getPost('password'));
                $user->email = $this->request->getPost('email');
                $user->created_at = time();
                $user->active = 'Y';
                if (UserMongo::register($user)) {
                    $this->response->redirect('/logs');
                }
                $error = $user->getLastError();
            } else {
                $error = '你提交的数据验证失败';
            }
        }
        $tokenKey = $this->security->getTokenKey();
        $tokenValue = $this->security->getToken();
        $this->view->setVars([
            'username' => $user->username,
            'name' => $user->name,
            'email' => $user->email,
            'tokenKey' => $tokenKey,
            'tokenValue' => $tokenValue
        ]);
        $this->view->setVar('error', $error);
        return $this->view->render('site', 'register');
    }
    public function loginAction()
    {
        if ($this->session->get('user')) {
            throw new \Exception('Your Not Allowed Access This Page!');
        }
        $error = '';
        $userName = '';
        $failCount = $this->session->get('failedCount');
        if ($this->request->isPost()) {
            if ($this->security->checkToken()) {
                $userName = $this->request->getPost('username');
                $password = $this->request->getPost('password');
                $captcha = null;
                if ($failCount > 2) {
                    $captcha = strval($this->request->getPost('captcha'));
                }
                if (!empty($userName) && !empty($password)) {
                    if ($user = User::login($userName, $password, $captcha)) {
                        return $this->response->redirect('/logs');
                    } else {
                        $error = User::$error;
                    }
                } else {
                    $error = '用户名或密码为空';
                }
            } else {
                $error = '你提交的数据无法验证';
            }
            ++$failCount;
            $this->session->set('failedCount', $failCount);
        }
        $this->view->setVar('failCount', $failCount);
        $this->view->setVar('error', $error);
        $this->view->setVar('username', $userName);
        return $this->view->render('site', 'login');
    }

    public function logoutAction()
    {
        $this->session->destroy(true);
    }

    public function captchaAction()
    {
        $captcha = new \Captcha();
        $captcha->genCaptcha();
//        $captcha->font = BASE_PATH . '/public/fonts/ztgjkt.ttf';
//        var_dump($captcha->font);exit;
//        $captcha->genMathCode();
    }

    public function testAction()
    {
        $mongo = $this->di->getShared('mongo');
        $mongo->execute('db.users.insertMany(
  [
     {
       _id: 1,
       name: "sue",
       age: 19,
       type: 1,
       status: "P",
       favorites: { artist: "Picasso", food: "pizza" },
       finished: [ 17, 3 ],
       badges: [ "blue", "black" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 85, bonus: 10 }
       ]
     },
     {
       _id: 2,
       name: "bob",
       age: 42,
       type: 1,
       status: "A",
       favorites: { artist: "Miro", food: "meringue" },
       finished: [ 11, 25 ],
       badges: [ "green" ],
       points: [
          { points: 85, bonus: 20 },
          { points: 64, bonus: 12 }
       ]
     },
     {
       _id: 3,
       name: "ahn",
       age: 22,
       type: 2,
       status: "A",
       favorites: { artist: "Cassatt", food: "cake" },
       finished: [ 6 ],
       badges: [ "blue", "red" ],
       points: [
          { points: 81, bonus: 8 },
          { points: 55, bonus: 20 }
       ]
     },
     {
       _id: 4,
       name: "xi",
       age: 34,
       type: 2,
       status: "D",
       favorites: { artist: "Chagall", food: "chocolate" },
       finished: [ 5, 11 ],
       badges: [ "red", "black" ],
       points: [
          { points: 53, bonus: 15 },
          { points: 51, bonus: 15 }
       ]
     },
     {
       _id: 5,
       name: "xyz",
       age: 23,
       type: 2,
       status: "D",
       favorites: { artist: "Noguchi", food: "nougat" },
       finished: [ 14, 6 ],
       badges: [ "orange" ],
       points: [
          { points: 71, bonus: 20 }
       ]
     },
     {
       _id: 6,
       name: "abc",
       age: 43,
       type: 1,
       status: "A",
       favorites: { food: "pizza", artist: "Picasso" },
       finished: [ 18, 12 ],
       badges: [ "black", "blue" ],
       points: [
          { points: 78, bonus: 8 },
          { points: 57, bonus: 7 }
       ]
     }
  ]
)');
    }
}